package org.sgx.gapi.client.apis.tasks.tasklists;

import org.sgx.gapi.client.GAPI;
import org.sgx.gapi.client.client.ClientRequest;
import org.sgx.gapi.client.client.ClientRequestCallback;
import org.sgx.gapi.client.client.GAPICallback;
import org.sgx.gapi.client.client.GAPIListRequest;
import org.sgx.gapi.client.client.GAPIRequestAbstract;
import org.sgx.gapi.client.client.GAPIResult;
import org.sgx.gapi.client.client.HttpRequest;
import org.sgx.jsutil.client.JsObject;

/**
 * Creates a new task list and adds it to the authenticated user's task lists
 * @see https://developers.google.com/google-apps/tasks/v1/reference/tasklists/insert
 * @author sg
 * 
 */
public class InsertRequest
extends GAPIRequestAbstract<TaskList> {
	TaskList taskList;
	
	private InsertRequest(TaskList taskList) {
		super();
		this.taskList = taskList;
	}

	@Override
	public void execute(final GAPICallback<TaskList> c) {
		JsObject param = getParams();// JsObject.createObject().cast(); //getListParams();
		
		ClientRequest clientReq = ClientRequest.create()
				.path("/tasks/v1/users/@me/lists")
				.params(param)
				.method("post")
				.body(taskList);
		HttpRequest req = GAPI.get().client().request(clientReq);
		req.execute(new ClientRequestCallback() {
			@Override
			public void call(GAPIResult jsonResp, String rawResp) {
				c.call((TaskList) jsonResp.cast());
			}
		});
	}

}
